use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;
use sha2::{Sha256, Digest};

#[derive(Queryable, Serialize, Deserialize)]
pub struct User {
    pub id: i32,
    pub public_key: String,
    pub public_key_hash: String,
    pub address: String,
    pub phone_or_email: String,
    pub username: String,
    pub digital_signature: String,
}

#[derive(Deserialize)]
struct RegisterData {
    public_key: String,
    address: String,
    phone_or_email: String,
    username: String,
    digital_signature: String,
}

async fn register_user(data: web::Json<RegisterData>) -> impl Responder {
    let public_key_hash = Sha256::new()
        .chain(data.public_key.as_bytes())
        .finalize()
        .to_vec();

    // 这里应该添加逻辑来生成唯一的用户ID和处理数字签名

    HttpResponse::Ok().json({
        // 返回注册成功的消息
        "Registration successful"
    })
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/register", web::post().to(register_user))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}
